import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import com.epower.dao.epowerword.EpowerWordDAO;
import com.epower.dataobject.EpowerWordDO;
import com.epower.utils.HttpUtils;

/**
 * @author bolin.hbc
 * 
 *         获取Google的图片
 * 
 */
public class CatchGoogleImage extends BaseTestCase {
	public static final String GOOGLE_IMG_URL_SEARCH = "http://cn.bing.com/images/search?q=";

	public EpowerWordDAO epowerWordDAO;

	static ArrayBlockingQueue<String> arrayBlock = new ArrayBlockingQueue<String>(500);
	public static final String STORE_IMG_PATH = "D:\\images";

	public static final String ERROR_IMG_PATH = "D:\\images\\err.txt";
	public static File file = new File(ERROR_IMG_PATH);
	public static BufferedWriter b;

	public void test1() throws InterruptedException, IOException {
		file.createNewFile();
		b = new BufferedWriter(new FileWriter(file));

		int pages = (508322 / 10000) + 1;
		Set<String> set = new HashSet<String>();
		ExecutorService pool = Executors.newFixedThreadPool(50);
		for (int i = 0; i < 20; i++) {
			pool.execute(new ThreadPoolTest());
		}
		for (int i = 1; i <= pages; i++) {
			EpowerWordDO e = new EpowerWordDO();
			e.setToPage(i);
			e.setPerPageSize(10000);
			List<EpowerWordDO> words = epowerWordDAO.getEpowerWordListByOption(e);
			for (EpowerWordDO w : words) {
				String s = w.getSpelling().trim();
				if (set.add(s)) {
					arrayBlock.offer(s, 600, TimeUnit.SECONDS);
				}
			}

		}
	}

	public void setEpowerWordDAO(EpowerWordDAO epowerWordDAO) {
		this.epowerWordDAO = epowerWordDAO;
	}

	class ThreadPoolTest implements Runnable {
		public void run() {
			String s = "";
			try {
				while ((s = arrayBlock.poll(600, TimeUnit.SECONDS)) != null) {
					String key = "";
					try {
						s = arrayBlock.poll(600, TimeUnit.SECONDS);

						if (s == null && arrayBlock.size() == 0) {
							break;
						}
						File fileP = new File("D:\\images" + "\\" + s.substring(0, 1));
						if (!fileP.exists()) {
							fileP.mkdir();

						}
						File file = new File("D:\\images" + "\\" + s.substring(0, 1) + "\\" + s + ".jpg");

						if (file.exists()) {
							continue;
						}
						file.createNewFile();

						String web = HttpUtils.getResponse("http://cn.bing.com/images/search?q=" + s, "utf-8");
						key = web.substring(web.indexOf("imgurl:&quot;http://") + 13, web.indexOf("&quot;,offset:&quot;0"));
						if (key == null || key.isEmpty()) {

							continue;
						}
						URL url;

						url = new URL(key);

						HttpURLConnection connection = (HttpURLConnection) url.openConnection();
						connection.connect();
						OutputStream os = null;
						InputStream is = null;
						is = connection.getInputStream();
						file.createNewFile();
						os = new FileOutputStream(file);
						int b = is.read();
						while (b != -1) {
							os.write(b);
							b = is.read();
						}
						is.close();
						os.close();
					} catch (Exception e1) {
						file.delete();
						try {
							b.append("error:" + s + "  url is: " + key + " " + e1.getMessage()).append("\n");
							b.flush();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}

				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
	}

	public static void main(String[] args) {
		System.out.println("https://encrypted-tbn2.google.com/images?q\\x3dtbn:");
		System.out.println("\",\"\",\"\",\"/search?q");
	}
}

abstract class BaseTestCase extends AbstractDependencyInjectionSpringContextTests {
	protected final Log log = LogFactory.getLog(getClass());

	public BaseTestCase() {
		setDependencyCheck(false);
		super.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
	}

	protected String[] getConfigLocations() {
		return new String[] { "classpath*:/spring-test-ds.xml" };
	}
}